<HTML>
<HEAD>
<TITLE>[Chapter 5] 5.5 Canvas</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:34:37 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5<br>Components</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-5-SECT-5">5.5 Canvas</A></h2>

<P CLASS=para>
A <tt CLASS=literal>Canvas</tt> is a class just waiting 
to be subclassed. Through <tt CLASS=literal>Canvas</tt>, 
you can create additional AWT objects that are not provided by the base 
classes. <tt CLASS=literal>Canvas</tt> is also useful 
as a drawing area, particularly when additional components are on the screen. 
It is tempting to draw directly onto a <tt CLASS=literal>Container</tt>, but this often isn't 
a good idea. Anything you draw might disappear underneath the components 
you add to the container. When you are drawing on a container, you are 
essentially drawing on the background. The container's layout manager 
doesn't know anything about what you have drawn and won't 
arrange components with your artwork in mind. To be safe, do your drawing 
onto a <tt CLASS=literal>Canvas</tt> and place that 
<tt CLASS=literal>Canvas</tt> in a <tt CLASS=literal>Container</tt>. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-5-SECT-5.1">Canvas Methods</A></h3>Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Canvas () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The constructor creates a new <tt CLASS=literal>Canvas</tt> 
with no default size. If you place the canvas in a container, the container's 
layout manager sizes the canvas for you. If you aren't placing the 
canvas in a container, call <tt CLASS=literal>setBounds()</tt> 
to specify the canvas's size. 

<P CLASS=para>
Java 1.0 used the default constructor for <tt CLASS=literal>Canvas</tt>; 
there was no explicit constructor. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public void paint (Graphics g) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The default implementation of the <tt CLASS=literal>paint()</tt> 
method colors the entire <tt CLASS=literal>Canvas</tt> 
with the current background color. When you subclass this method, your 
<tt CLASS=literal>paint()</tt> method needs to draw 
whatever should be shown on the canvas. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void addNotify () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addNotify()</tt> method creates 
the <tt CLASS=literal>Canvas</tt> peer. If 
you override this method, first call <tt CLASS=literal>super.addNotify()</tt>, 
then add your customizations. Then you can do everything you need with 
the information about the newly created peer. </DL>
</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-5-SECT-5.2">Canvas Events</A></h3>

<P CLASS=para>
The <tt CLASS=literal>Canvas</tt> peer passes all 
events to you, which is why it's well suited to creating your own 
components. 

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>A Simple Calculator</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Creating Your Own Component</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
